package com.hcxy.filetransform.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.hcxy.filetransform.TranslateActivity;
import com.hcxy.filetransform.domain.TransformLog;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

public class  DbUtil{
	
	private static DbUtils _db;
	public static DbUtils getDb(Context context){
		if (null==_db) {
			_db = DbUtils.create(context);
		}
		return _db;
	}
	public static List<TransformLog> getTransformLogs(Context context,String mac,int start,int size) throws DbException{
		DbUtils db = getDb(context);
		List<TransformLog> data = db.findAll(Selector.from(TransformLog.class).where("mac", "=", mac).orderBy("date",true).offset(start).limit(size));
		return data;
	}
	
	public static List<TransformLog> loadLogs(Context context,String mac,TransformLog log) throws DbException{
		DbUtils db = getDb(context);
		if (log==null) {
			List<TransformLog> data = db.findAll(Selector.from(TransformLog.class).where("mac", "=", mac).orderBy("date",true).limit(10));
			return data;
		}else{
			List<TransformLog> data = db.findAll(Selector.from(TransformLog.class).where("mac", "=", mac)
					.and("date", "<", log.getDate()).orderBy("date",true).limit(10));
			return data;
		}
	}
	
	public static void addTransformLog(Context context,TransformLog log) throws DbException{
		DbUtils db = getDb(context);
		db.saveOrUpdate(log);
	}
	
	public static void bindLog(Context context,TransformLog log){
		DbUtils db = getDb(context);
		try {
			db.saveBindingId(log);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	
	public static void updateLog(Context context,TransformLog log){
		DbUtils db = getDb(context);
		try {
			db.saveOrUpdate(log);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
}
